﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace FacebookApp
{
	public class GroupUserService
	{

		#region[GroupUser_GetbyAll]
        public static List<GroupUser> GroupUser_GetByAll()
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from pu in dt.GroupUsers select pu).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[GroupUser_GetbyId]
        public static GroupUser GroupUser_GetById(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from pu in dt.GroupUsers where pu.Id == id select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[GroupUser_GetbyTop]
        public static List<GroupUser> GroupUser_GetByTop(string Top, string Where, string Order)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [GroupUser]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [GroupUser] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<GroupUser> results = dt.ExecuteQuery<GroupUser>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
		}
		#endregion
		#region[GroupUser_Insert]
        public static void GroupUser_Insert(GroupUserInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    GroupUser ob = new GroupUser();
                    ob.Name = Data.Name;

                    dt.GroupUsers.InsertOnSubmit(ob);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[GroupUser_Update]
        public static void GroupUser_Update(GroupUserInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    GroupUser da = dt.GroupUsers.Single(au => au.Id == id);
                    da.Name = Data.Name;

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[GroupUser_Delete]
		public static void GroupUser_Delete(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    GroupUser da = dt.GroupUsers.Single(au => au.Id == id);
                    dt.GroupUsers.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
	}

    public class GroupUserInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }

    }
}